package GDoc::Job::Fetch::US::Judi::SCOTUS::SlipDecisions;

use Moose;
use HTML::TableContentParser;
use File::Temp;

extends 'GDoc::Job::Fetch';

sub setup { 
    my $self = shift;
    $self->_baseurl( 'http://www.supremecourtus.gov/opinions/%02dslipopinion.html' );
    $self->_basedir( 'http://www.supremecourtus.gov/opinions/' );
}

sub run { 
    my $self = shift;

    if ( $self->data->{link} ) { 
        $self->_fetch_pdf;
    } else { 
        # cycle through terms 2005-2008; earlier stuff is not available on SCOTUS website :(
        my $mech = $self->_mech;
        foreach my $term( 5 .. 8 ) { 
            my $idx_url = sprintf( $self->_baseurl, $term );
 
            $self->logger->info( "Fetching base $idx_url" );
            $mech->get( $idx_url );

            my $parser = HTML::TableContentParser->new;
            my $tbl = $parser->parse( $mech->response->content )->[4];   # 5th one down

            shift @{ $tbl->{rows} };                                     # throw away headers
            foreach my $row( @{ $tbl->{rows} } ) {
                my @cells = map { $_->{data} } @{ $row->{cells} };
                my $link = $cells[3];
                $link =~ m{<a href="(?<url>[\w/-]+\.pdf)">(?<name>.+?)</a>}i;
                my ( $url, $name ) = @+{ 'url', 'name' };

                s{</?p>}{}g for @cells;

                my $job = GDoc::Job->new( class => 'Fetch::US::Judi::SCOTUS::SlipDecisions',
                                          queue => $self->fetch_queue,
                                          data  => { recorder  => $cells[0],
                                                     docket    => $cells[2],
                                                     date      => $cells[1],
                                                     desc      => $name,
                                                     link      => $url
                                                   }
                                        );

                eval { 
                    $job->submit;
                };

                if ( my $err = $@ ) { 
                    ### FIX: A few of these cause SQS to blow up because of 
                    ###      character encoding issues; log the error for 
                    ###      now - fix later
                    $self->logger->error( $err );
                }
            }
        }
    }
}


sub _fetch_pdf { 
    my $self = shift;
    my %data = %{ $self->data };

    $data{docket} =~ m{(?<year>\d{2})-(?<num>\d{1,4})};
    my ( $year, $num ) = @+{ 'year', 'num' };
    $year += 2000;    

    my $ident = sprintf( 'SC-SO-%04d-%04d', $year, $num );
    my $filename = $ident . '.pdf';
    my $key = 'us/judi/scotus/' . $filename;

    $self->logger->info( "Storing $data{link} --> $key" );

    my $mech = $self->_mech;
    my $tmpf = File::Temp->new;
    $mech->get( $self->_basedir . $data{link}, ':content_file' => $tmpf->filename );

    $self->store_pdf( key       => $key,
                      datafile  => $tmpf,
                      filename  => $filename,
                      meta      => { description  => $data{desc},
                                     identifier   => $ident,
                                     recorder_num => $data{recorder},
                                     date         => $data{date}
                                   }
                    );
}

1;
